From caef41000f0478b6380b10d88245cf95757b2af6 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 2 Jun 2015 15:20:50 +0200 Subject: [PATCH] Update for windows (WIP) --- src/cargo/util/shell_escape.rs | 74 +++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/src/cargo/util/shell_escape.rs b/src/cargo/util/shell_escape.rs index 39fb603bd..9584794da 100644 --- a/src/cargo/util/shell_escape.rs +++ b/src/cargo/util/shell_escape.rs @@ -8,36 +8,54 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::borrow::Cow; - -static SHELL_SPECIAL: &'static str = r#" \$'"`!"#; - -/// Escape characters that may have special meaning in a shell, -/// including spaces. -pub fn shell_escape(s: Cow) -> Cow { - let escape_char = '\\'; - // check if string needs to be escaped - let clean = SHELL_SPECIAL.chars().all(|sp_char| !s.contains(sp_char)); - if clean { - return s +#[cfg(not(target_os = "windows"))] +pub use self::windows::shell_escape; + + +mod windows { + use std::borrow::Cow; + + pub fn shell_escape(s: Cow) -> Cow { + panic!() } - let mut es = String::with_capacity(s.len()); - for ch in s.chars() { - if SHELL_SPECIAL.contains(ch) { - es.push(escape_char); +} + +#[cfg(target_os = "windows")] +pub use self::other::shell_escape; + +mod other { + use std::borrow::Cow; + + static SHELL_SPECIAL: &'static str = r#" \$'"`!"#; + + #[cfg(not(target_os = "windows"))] + /// Escape characters that may have special meaning in a shell, + /// including spaces. + pub fn shell_escape(s: Cow) -> Cow { + let escape_char = '\\'; + // check if string needs to be escaped + let clean = SHELL_SPECIAL.chars().all(|sp_char| !s.contains(sp_char)); + if clean { + return s + } + let mut es = String::with_capacity(s.len()); + for ch in s.chars() { + if SHELL_SPECIAL.contains(ch) { + es.push(escape_char); + } + es.push(ch) } - es.push(ch) + es.into() } - es.into() -} -#[test] -fn test_shell_escape() { - assert_eq!(shell_escape("--aaa=bbb-ccc".into()), "--aaa=bbb-ccc"); - assert_eq!(shell_escape("linker=gcc -L/foo -Wl,bar".into()), - r#"linker=gcc\ -L/foo\ -Wl,bar"#); - assert_eq!(shell_escape(r#"--features="default""#.into()), - r#"--features=\"default\""#); - assert_eq!(shell_escape(r#"'!\$`\\\n "#.into()), - r#"\'\!\\\$\`\\\\\\n\ "#); + #[test] + fn test_shell_escape() { + assert_eq!(shell_escape("--aaa=bbb-ccc".into()), "--aaa=bbb-ccc"); + assert_eq!(shell_escape("linker=gcc -L/foo -Wl,bar".into()), + r#"linker=gcc\ -L/foo\ -Wl,bar"#); + assert_eq!(shell_escape(r#"--features="default""#.into()), + r#"--features=\"default\""#); + assert_eq!(shell_escape(r#"'!\$`\\\n "#.into()), + r#"\'\!\\\$\`\\\\\\n\ "#); + } } -- 2.30.2